//
//  MMGPrizeClawOpenGLView.m
//  MyApp
//
//  Created by Don on 2017/8/24.
//
//

#import "MMGPrizeClawOpenGLView.h"
#include <OpenGLES/ES2/gl.h>
#include <OpenGLES/ES2/glext.h>
#include "glrenderer.h"

@interface MMGPrizeClawOpenGLView () {
    EAGLContext                 *_context;
    GLuint                      _framebuffer;
    GLuint                      _renderbuffer;
    
    GLint                       _backingWidth; // UIView width
    GLint                       _backingHeight; // UIView height
}
@end
@implementation MMGPrizeClawOpenGLView

+ (Class)layerClass {
    return [CAEAGLLayer class];
}

- (instancetype)initWithFrame:(CGRect)frame {

    if ((self = [super initWithFrame:frame])) {
        
        CAEAGLLayer *glLayer = (CAEAGLLayer *)self.layer;
        glLayer.opaque = YES;
        glLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                      [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking,
                                      kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
                                      nil];
        
        
        EAGLRenderingAPI rendingAPI = kEAGLRenderingAPIOpenGLES2;
        _context = [[EAGLContext alloc] initWithAPI:rendingAPI];
        if (!_context ||
            ![EAGLContext setCurrentContext:_context]) {
            
            DLog(@"failed to setup EAGLContext\n");
            self = nil;
            return nil;
        }
        
        //渲染缓存区绑定
        glGenRenderbuffers(1, &_renderbuffer);
        glBindRenderbuffer(GL_RENDERBUFFER, _renderbuffer);
        [_context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)self.layer];
        glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &_backingWidth);
        glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &_backingHeight);
        
        glGenFramebuffers(1, &_framebuffer);
        glBindFramebuffer(GL_FRAMEBUFFER, _framebuffer);
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _renderbuffer);
        
        GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
        if (status != GL_FRAMEBUFFER_COMPLETE) {
            
            DLog(@"failed to make complete framebuffer object %x\n", status);
            self = nil;
            return nil;
        }
        
        GLenum glError = glGetError();
        if (GL_NO_ERROR != glError) {
            
            DLog(@"failed to setup GL %x\n", glError);
            self = nil;
            return nil;
        }

        
        //因为缺省的话，CALayer是透明的。而透明的层对性能负荷很大，特别是OpenGL的层
        [self setupLayer];
        [self cleanRender];
        //渲染配置
        renderer_init(_context , (CAEAGLLayer*)self.layer);
    }
    
    return self;
}

- (void)dealloc {
    if ([EAGLContext currentContext] == _context) {
        [EAGLContext setCurrentContext:nil];
    }
}
- (void)setupLayer {
    CAEAGLLayer *_eaglLayer = (CAEAGLLayer*) self.layer;
    _eaglLayer.opaque = YES;
}


//- (void)setRenderSize:(CGSize)vSize {
//    
//    renderer_setsize(vSize.width, vSize.height, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
//}

- (void)renderGLWithFrame:(uint8_t *)frame {
    
    renderer_renderframe(frame);
    
}
- (void)cleanRender {
    glBindFramebuffer(GL_FRAMEBUFFER, _framebuffer);
    glViewport(0, 0, _backingWidth, _backingHeight);
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    
    [_context presentRenderbuffer:GL_RENDERBUFFER];
    
}
@end
